//
// Created by 32827 on 2023/11/27.
//
#include "yz_recv_transmit.h"
/**
 * channel 1~10 USART/UART
 * channel 11~20 SPI
 * channel 21~30 IIC
 * channel 31~40 CAN
 * channel 41~50 USB
 * channel 51~255 not define
 * */
uint8_t m_yz_recv_transmit_channel=0;
YZF_RECV *m_yz_recv_transmit_channel_pointer;

YZF_GLOBAL_INDEX m_yz_recv_transmit_handler_process;
YZF_GLOBAL_INDEX m_yz_recv_transmit_now_handler_process;

uint32_t m_yz_recv_transmit_handler_sheet[YZ_RECV_TRANSMIT_HANDLER_SHEET_SIZE]={YZF_NULL_PTR};
YZF_RESULT yz_recv_transmit_channel(void *channel){
    switch ((uint32_t) channel) {
        case (uint32_t)USART1:
            m_yz_recv_transmit_channel=1;
            m_yz_recv_transmit_channel_pointer= yz_uart_receive_IT_RECV_get(channel);
            return YZF_OK;
        case (uint32_t)USART2:
            m_yz_recv_transmit_channel=2;
            m_yz_recv_transmit_channel_pointer= yz_uart_receive_IT_RECV_get(channel);
            return YZF_OK;
        default:
            m_yz_recv_transmit_channel=0;
            return YZF_NOT_FOUND;
    }
}
/**
 * 初始化接收数据的通道 目前已定义USART1 USART2,其他自行定义
 * */
YZF_RESULT yz_recv_transmit_init(void *channel){
    YZF_RESULT re=yz_recv_transmit_channel(channel);
    if(re!=YZF_OK){
        return re;
    }else{
        m_yz_recv_transmit_handler_process=yzf_global_handler_add(yz_recv_transmit_handler_process);
        yz_recv_transmit_reset();
        return YZF_OK;
    }
}
/**
 * 重置为主处理函数的句柄
 * */
YZF_RESULT yz_recv_transmit_reset(){
    m_yz_recv_transmit_now_handler_process=m_yz_recv_transmit_handler_process;
    yzf_area_recv_del(m_yz_recv_transmit_channel_pointer,yzf_area_recv_get_size(m_yz_recv_transmit_channel_pointer));
    YZ_RECV_TRANSMIT_LEW_LIGHT;
    return YZF_OK;
}
/**
 * 是否已经重置为主处理函数的句柄
 * */
YZF_BOOL yz_recv_transmit_isReset(){
    if(m_yz_recv_transmit_now_handler_process==m_yz_recv_transmit_handler_process){
        return YZF_TRUE;
    }
    return YZF_FALSE;
}
/**
 * 设置处理函数的句柄
 * */
YZF_RESULT yz_recv_transmit_handler_set(YZF_GLOBAL_INDEX handlerCode,YZF_RESULT (*process)()){
    if(YZ_RECV_TRANSMIT_HANDLER_SHEET_SIZE<=handlerCode){
        return YZF_NOT_FOUND;
    }
    m_yz_recv_transmit_handler_sheet[handlerCode]=(uint32_t)process;
    return YZF_OK;
}
/**
 * 接收与全局循环函数，通过数据传入调度函数
 * */
YZF_RESULT yz_recv_transmit_handler_process(){
    if(!yz_recv_transmit_isReset()){
        return YZF_HANDLER_NOT_FINISH;
    }
    if(yzf_area_recv_get_size(m_yz_recv_transmit_channel_pointer)>=4){
        uint32_t *pdata= (uint32_t *)yzf_area_recv_get_read_ptr(m_yz_recv_transmit_channel_pointer);
        if(*pdata>=YZ_RECV_TRANSMIT_HANDLER_SHEET_SIZE){
            yzf_area_recv_del(m_yz_recv_transmit_channel_pointer,4);
            return YZF_HANDLER_NOT_FINISH;
        }
        if(m_yz_recv_transmit_handler_sheet[*pdata]!=YZF_NULL_PTR){
            YZ_RECV_TRANSMIT_LEW_DARK;
            m_yz_recv_transmit_now_handler_process=yzf_global_handler_add((YZF_RESULT (*)())m_yz_recv_transmit_handler_sheet[*pdata] );
        }
        yzf_area_recv_del(m_yz_recv_transmit_channel_pointer,4);
    }else {
        return YZF_NOTHING;
    }
    return YZF_HANDLER_NOT_FINISH;
}
/**
 * 获取数据接收保存区域
 * */
YZF_RECV * yz_recv_transmit_area_get(){
    return m_yz_recv_transmit_channel_pointer;
}
/**
 * 返回单次线性传输的最大数量
 * */
YZF_RECV_DATA_INDEX yz_recv_transmit_once_line_most() {
    uint32_t pst = (uint32_t) yzf_area_recv_get_read_ptr(m_yz_recv_transmit_channel_pointer);
    uint32_t pse = (uint32_t) yzf_area_recv_get_end_ptr(m_yz_recv_transmit_channel_pointer);
    return pse - pst;
}